import { store } from '@/store'
import { ref } from 'vue'
export const jurisdictionData = ref()
// getUser().then((res) => {
//   jurisdictionData.value = res.data.data.authority
// })
const handleLockPerms = () => {
  // 保存解锁/锁定权限
  let lockPermsList = []
  jurisdictionData.value.forEach((d) => {
    if (d.perms?.includes(':lock') && d.hasRight === 1) {
      lockPermsList.push(d.perms)
    }
  })
  store.commit('saveLockPermsList', lockPermsList)
}
export default (Vue, params) => {
  jurisdictionData.value = params
  handleLockPerms()

  let isExist = true
  Vue.directive('limit', {
    mounted(el, binding) {
      // //获取按钮权限
      if (Vue.config.globalProperties.$_has(binding.value[0])) {
        //移除不匹配的按钮
        el.parentNode.removeChild(el)
      }
    },
  })
  // //检查权限方法
  Vue.config.globalProperties.$_has = function (value) {
    if (jurisdictionData.value == undefined) {
      isExist = true
    } else {
      for (let item of jurisdictionData.value) {
        if (item.perms === value && item.hasRight === 1) {
          isExist = false
          return
        } else {
          if (item.perms?.includes(':lock')) {
            return false
          }
          isExist = true
        }
      }
    }
    return isExist
  }
}